package system1

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system1"
	system1Req "github.com/flipped-aurora/gin-vue-admin/server/model/system1/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type Sys_departmentApi struct {
}

var departmentService = service.ServiceGroupApp.System1ServiceGroup.Sys_departmentService

// CreateSys_department 创建部门
// @Tags Sys_department
// @Summary 创建部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.Sys_department true "创建部门"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /department/createSys_department [post]
func (departmentApi *Sys_departmentApi) CreateSys_department(c *gin.Context) {
	var department system1.Sys_department
	err := c.ShouldBindJSON(&department)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	department.CreatedBy = utils.GetUserID(c)

	if err := departmentService.CreateSys_department(&department); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteSys_department 删除部门
// @Tags Sys_department
// @Summary 删除部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.Sys_department true "删除部门"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /department/deleteSys_department [delete]
func (departmentApi *Sys_departmentApi) DeleteSys_department(c *gin.Context) {
	ID := c.Query("ID")
	userID := utils.GetUserID(c)
	if err := departmentService.DeleteSys_department(ID, userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteSys_departmentByIds 批量删除部门
// @Tags Sys_department
// @Summary 批量删除部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /department/deleteSys_departmentByIds [delete]
func (departmentApi *Sys_departmentApi) DeleteSys_departmentByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	userID := utils.GetUserID(c)
	if err := departmentService.DeleteSys_departmentByIds(IDs, userID); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateSys_department 更新部门
// @Tags Sys_department
// @Summary 更新部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body system1.Sys_department true "更新部门"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /department/updateSys_department [put]
func (departmentApi *Sys_departmentApi) UpdateSys_department(c *gin.Context) {
	var department system1.Sys_department
	err := c.ShouldBindJSON(&department)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	department.UpdatedBy = utils.GetUserID(c)

	if err := departmentService.UpdateSys_department(department); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindSys_department 用id查询部门
// @Tags Sys_department
// @Summary 用id查询部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1.Sys_department true "用id查询部门"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /department/findSys_department [get]
func (departmentApi *Sys_departmentApi) FindSys_department(c *gin.Context) {
	ID := c.Query("ID")
	if redepartment, err := departmentService.GetSys_department(ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redepartment": redepartment}, c)
	}
}

// GetSys_departmentList 分页获取部门列表
// @Tags Sys_department
// @Summary 分页获取部门列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query system1Req.Sys_departmentSearch true "分页获取部门列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /department/getSys_departmentList [get]
func (departmentApi *Sys_departmentApi) GetSys_departmentList(c *gin.Context) {
	var pageInfo system1Req.Sys_departmentSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := departmentService.GetSys_departmentInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
